import { createApp } from 'vue'

import Cookies from 'js-cookie'
import ElementPlus from 'element-plus'
import VXETable from 'vxe-table'
// import locale from 'element-plus/es/locale/lang/zh-cn' // 中文语言
import zhCn from 'element-plus/es/locale/lang/zh-cn' // 中文语言
import "tailwindcss/tailwind.css"
import '@/assets/icons/iconfont/index.css'
import '@/assets/styles/index.scss' // global css
import 'element-plus/dist/index.css'
// import 'element-plus/theme-chalk/dark/css-vars.css'
import '@/assets/styles/css-vars.scss'

import App from './App'
import store from './store'
import router from './router'
import directive from './directive' // directive

// 注册指令
import plugins from './plugins' // plugins
import { download } from '@/utils/request'

// svg图标
import 'virtual:svg-icons-register'
import SvgIcon from '@/components/SvgIcon'
import elementIcons from '@/components/SvgIcon/svgicon'

import './permission' // permission control
import './config' // 网关配置

import { useDict } from '@/utils/dict'
import { parseTime, resetForm, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi'
import VueMatomo from 'vue-matomo'

// 分页组件
import Pagination from '@/components/Pagination'
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar'
// 富文本组件
import Editor from "@/components/Editor"
// 文件上传组件
import FileUpload from "@/components/FileUpload"
// 图片上传组件
import ImageUpload from "@/components/ImageUpload"
// 图片预览组件
import ImagePreview from "@/components/ImagePreview"
// 自定义树选择组件
import TreeSelect from '@/components/TreeSelect'
// 字典标签组件
import DictTag from '@/components/DictTag'
import request from "./utils/request";
import globalUtils from "@/utils/global";
import downloadLabelInfo from "@/utils/global/downloadLabelInfo";
import routerPlugin from "@/utils/global/routerPlugin";

export const app = createApp(App)

// 全局方法挂载
app.config.globalProperties.useDict = useDict
app.config.globalProperties.download = download
app.config.globalProperties.parseTime = parseTime
app.config.globalProperties.resetForm = resetForm
app.config.globalProperties.handleTree = handleTree
app.config.globalProperties.addDateRange = addDateRange
app.config.globalProperties.selectDictLabel = selectDictLabel
app.config.globalProperties.selectDictLabels = selectDictLabels


// 全局组件挂载
app.component('DictTag', DictTag)
app.component('Pagination', Pagination)
app.component('TreeSelect', TreeSelect)
app.component('FileUpload', FileUpload)
app.component('ImageUpload', ImageUpload)
app.component('ImagePreview', ImagePreview)
app.component('RightToolbar', RightToolbar)
app.component('Editor', Editor)
app.use(router)
app.use(store)
app.use(VXETable)
app.use(plugins)
app.use(elementIcons)
app.component('svg-icon', SvgIcon)

import useUserStore from '@/store/modules/user'
import useMatomo from '@/plugins/matomo.ts'
const userData = useUserStore()
const matomos = useMatomo()
document.getElementById("main-app").onclick = (e) => {
  if(e?.target?.localName == 'button' || e?.target?.parentElement?.localName == 'button'){
    // 当前操作页名称
    let pageTitle = document.getElementsByClassName('matomo-page-title router-link-active router-link-exact-active active tags-view-item')?.[0]?.innerText || '暂无'
    matomos.setUserId(`${userData.name}(${userData.user_id})`)
    matomos.trackPageView(pageTitle)
    matomos.trackEvent('点击操作',e?.target?.innerText, '-')
  }
}





import * as Sentry from "@sentry/vue";
Sentry.init({
  app,
  dsn: "http://790b522c2861c06d4df44df26fcc2b23@10.20.9.199:9000/12",
  integrations: [
    Sentry.browserTracingIntegration({ router }),
    Sentry.replayIntegration(),
  ],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Set `tracePropagationTargets` to control for which URLs trace propagation should be enabled
  tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

app.use(VueMatomo, {
    // Configure your matomo server and site by providing
    host: import.meta.env.VITE_MATOMO_HOST,
    siteId: 1,
    // 根据router自动注册
    router: router,
    // // 是否需要在发送追踪信息之前请求许可 默认false
    requireConsent: false,

    enableLinkTracking: true,

    // // 是否追踪初始页面 默认true
    trackInitialView: false,
    // // 最终的追踪js文件名  默认 'piwik'
    // trackerFileName: 'matomo',
    debug: false,
    userId: ''
})

window.$httpRequest = request
window.__POWERED_BY_QIANKUN__ = true

// 下载标签信息
globalUtils.pushUtil(downloadLabelInfo, 'downloadLabelInfo')
// 手动实现router.push  用window.history.pushState替代
globalUtils.pushUtil(routerPlugin, 'routerPlugin')

directive(app)

// 使用element-plus 并且设置全局的大小
app.use(ElementPlus, {
  locale: zhCn,
  // 支持 large、default、small
  size: Cookies.get('size') || 'default',
})

app.mount('#main-app')

// window._paq.push(['trackPageView']); //To track pageview


